package pong.lan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import pong.rmi.ComRemota;

public class LanUpdater extends Thread{
	@Override
	public void run(){
		while(true){
			try {Thread.sleep(1000);
			} catch (Exception e) {}
			try{
				HashMap<String, ComRemota> compus =  Lan.compus;
				List<String> toDelete = new ArrayList<>();
				synchronized (Lan.lock) {
					for(String x: compus.keySet()){
						ComRemota com = compus.get(x);
						try{
							com.pruebaConexion();
						}catch(Exception e){
							toDelete.add(x);
						}
					}
					if(!toDelete.isEmpty()){
						for(String x: toDelete){
							compus.remove(x);
						}
						Lan.actualizarLista();
					}
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
